package com.hey.baseapplication.activity;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.hey.baseapplication.R;
import com.hey.baseapplication.presenter.BasePresenter;

/**
 * Created by Hey on 16/12/15.
 */

//这里为了MVP架构简方便,利用了泛型,在OnDestroy()中接触presenter的引用
public abstract  class BaseActivity<T extends BasePresenter> extends AppCompatActivity implements View.OnClickListener{
    protected T presenter;
    private ViewStub mContentView;
    private boolean isActionBar=true;
    protected Toolbar toolbar;

    private static  final  String TAG="BaseActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //锁定竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        presenter=initPresenter();
    }
    
    @Override
    public void setContentView(@LayoutRes int layoutResID) {
            if (isActionBar){
                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View baseContent = inflater.inflate(R.layout.activity_base, null);
                toolbar= (Toolbar) baseContent.findViewById(R.id.tool_bar);
                mContentView = (ViewStub) baseContent.findViewById(R.id.layout_container);
                mContentView.setLayoutResource(layoutResID);
                mContentView.inflate();
                super.setContentView(baseContent);
            }else{
                super.setContentView(layoutResID);
            }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            //透明导航栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG,"onResume");
        ActivityManager.instance.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        Log.e(TAG,"onDestroy");
        if (presenter!=null){
            presenter.destroy();
            presenter=null;
        }
        ActivityManager.instance.removeActivity(this);
        super.onDestroy();
    }


    protected void initView(){
        Log.e(TAG,"initView");
        findView();
        initData();
        setListener();

    }
    protected  abstract  void findView();
    protected  abstract  void initData();
    protected  abstract  void setListener();
    protected  T initPresenter(){
        return null;
    }



    /**
     * 设置标题
     */
    public void setTitle(@StringRes  int titleId) {
        setTitle(getString(titleId));
    }

    /**
     * 设置标题
     */
    public void setTitle(String title) {
        if (toolbar!=null){
            toolbar.setTitle(title);
        }
    }


    public void setIsActionBar(boolean isActionBar){
        this.isActionBar=isActionBar;
    }

    /**
     * 获取屏幕的宽度(px)
     * @return width
     */
    public int getScreenWidth(){
        DisplayMetrics displayMetrics=new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.widthPixels;
    }

    /**
     * 获取屏幕的高度(px)
     * @return height
     */
    public int getScreenHeight(){
        DisplayMetrics displayMetrics=new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.heightPixels;
    }

    /**
     * 获取网络连接状态
     * @return
     */
    public int getNetWorkState() {
        ConnectivityManager manager= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        if (manager.getActiveNetworkInfo()!=null&&manager.getActiveNetworkInfo().isAvailable()) {
           return manager.getActiveNetworkInfo().getType();
        }
        return 0;
    }



    protected <T extends View> T f(int id) {
        return (T) super.findViewById(id);
    }


    public void showToast(String message){
        if (isFinishing()){
            return;
        }
        if (!TextUtils.isEmpty(message)&&"".equals(message.trim())){
            Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
        }
    }


//    //沉浸式状态栏
//    @Override
//    public void onWindowFocusChanged(boolean hasFocus) {
//        super.onWindowFocusChanged(hasFocus);
//        if (hasFocus&& Build.VERSION.SDK_INT>=19){
//            View decorView = getWindow().getDecorView();
//            decorView.setSystemUiVisibility(
//                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE |  View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
//            Log.e("focusChange","1");
//        }
//    }
}
